package com.da.lon.wang.xlg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.appkefu.lib.interfaces.KFAPIs;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.PhoneConfirmBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.constants.XLGConstants.MethodName;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;


public class RegistActivity extends BaseActivity implements OnClickListener {

    private EditText usernameEdit;
    private TextView kefuTv;

    public static void startChat(Context context, String storecode,
                                 String storename, String useraccount, String usernickname,
                                 String goodsImageurl, String goodstitledetail, String goodsprice,
                                 String goodsurl) {

        boolean isEcChat = true;

        if (TextUtils.isEmpty(storecode)) {
            storecode = "pxokakfq";
        }
        if (TextUtils.isEmpty(storename)) {
            storename = "客服对话";
        }
        if (TextUtils.isEmpty(useraccount)) {
            useraccount = "游客";
        }
        if (TextUtils.isEmpty(usernickname)) {
            usernickname = "游客";
        }
        if (TextUtils.isEmpty(goodsImageurl)) {
            isEcChat = false;
        }
        if (TextUtils.isEmpty(goodstitledetail)) {
            isEcChat = false;
        }
        if (TextUtils.isEmpty(goodsprice)) {
            isEcChat = false;
        }
        if (TextUtils.isEmpty(goodsurl)) {
            isEcChat = false;
        }

        KFAPIs.setTagNickname(usernickname + "(" + useraccount + ")", context);
        if (!isEcChat) {
            KFAPIs.startChat(context, storecode, // 1.
                    // 客服工作组ID(请务必保证大小写一致)，请在管理后台分配
                    storename, // 2. 会话界面标题，可自定义
                    null,
                    false,
                    5, // 5. 默认显示消息数量
                    null, // 6.
                    null, // 7.
                    false, // 8. 默认机器人应答
                    isEcChat, null);
        } else {
            KFAPIs.startECChat(context, storecode, // 1.
                    // 客服工作组ID(请务必保证大小写一致)，请在管理后台分配
                    storename, // 2. 会话界面标题，可自定义
                    goodsurl,
                    false,
                    5,
                    null,
                    null,
                    false,

                    true,
                    goodsImageurl,
                    goodstitledetail,
                    goodsprice,

                    goodsurl, goodsurl, isEcChat, null);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xlg_regist);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initView() {
        ImageButton back = (ImageButton) findViewById(R.id.ouka_regist_left);
        back.setOnClickListener(this);
        ((Button) findViewById(R.id.next)).setOnClickListener(this);
        usernameEdit = (EditText) findViewById(R.id.username);
        kefuTv = (TextView) findViewById(R.id.kefu);
        kefuTv.setOnClickListener(this);
    }

    private void phoneConfirm() {
        String url = XLGConstants.URL + XLGConstants.URL_HEAD
                + MethodName.PHONE_CONFIRM;
        HttpLoader<PhoneConfirmBean> httpLoader = new HttpLoader<PhoneConfirmBean>(
                this, url, PhoneConfirmBean.class);
        AjaxParams params = new AjaxParams();
        params.put("phone", usernameEdit.getText().toString());
        params.put(
                "token",
                XLGDataUtils.encrypt2MD5(XLGConstants.API + XLGConstants.SECRET
                        + MethodName.PHONE_CONFIRM));
        params.put("device_type", "1");
        httpLoader
                .setManagerListener(new OnWebLoadListener<PhoneConfirmBean>() {

                    @Override
                    public void OnStart() {
                        showDialog("发送中");

                    }

                    @Override
                    public void OnError(String error) {
                        dismissDialog();
                        Toast.makeText(RegistActivity.this, "验证发送失败",
                                Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onSuccess(PhoneConfirmBean t) {
                        dismissDialog();
                        if (t != null && t.getCode().equals("000000")) {
                            Toast.makeText(RegistActivity.this, "验证已发送",
                                    Toast.LENGTH_SHORT).show();
                            PreferenceUtils.getInstance().SetSettingString(
                                    PreferencesConfig.USER_VERIFICATION,
                                    t.getResult().getPhone_vercode());
                            Intent confirmIntent = new Intent(
                                    RegistActivity.this,
                                    PhoneConfirmActivity.class);
                            confirmIntent.putExtra("phone", usernameEdit
                                    .getText().toString());
                            startActivity(confirmIntent);
                            finish();
                        } else {
                            Toast.makeText(RegistActivity.this, t.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                });

        httpLoader.loadData(params);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ouka_regist_left:
                finish();
                break;
            case R.id.next:
                if (TextUtils.isEmpty(usernameEdit.getText().toString())) {
                    Toast.makeText(this, getString(R.string.input_empty),
                            Toast.LENGTH_SHORT).show();
                } else if (usernameEdit.getText().toString().length() != 11
                        || !XLGDataUtils.isMobileNO(usernameEdit.getText()
                        .toString())) {
                    Toast.makeText(this, getString(R.string.phone_error),
                            Toast.LENGTH_SHORT).show();
                } else {
                    phoneConfirm();
                }

                break;
            case R.id.kefu:
                try {
                    startChat(this, "", "", "", "", "", "", "", "");
                } catch (Exception e) {
                    Toast.makeText(this, "参数有误", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }

    }
}
